#include<iostream>
using namespace std;
int arr[110];
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	int sum = 0;
	int min = 999;
	/*for (int i = 1; i <= n; i++) {
		if (arr[i] == 0 && i < n) {
			arr[i] = arr[i - 1] + 1;
			if (arr[i + 1] == 0) {
				arr[i + 1] = arr[i] + 1;
			}
			if (arr[i] >= arr[i + 1]) {
				cout << -1 << endl;
				return 0;
			}
		}
		if (arr[i] == 0 && i == n) {
			arr[i] = arr[i - 1] + 1;
		}
		sum += arr[i];
	}*/
	int count = 1;
	for (int i = 1; i <= n; i++) {
		count = 1;
		if (arr[i] == 0) {
			int j = i + 1;
			while (arr[j] == 0 && j<=n) {
				count++;
				arr[j] = arr[j - 1] + 1;
				j++;
			}
			if (arr[i]>=arr[i+1]) {
				cout << -1 << endl;
				return 0;
			}
			
		}
	}
	cout << sum << endl;
	return 0;
}